type TreeDataType = {
	[key: string]: any;
	children?: TreeDataType[];
};

// 传入树状数组，查找某个元素的所有直系父辈节点，返回数组。对象的查找标识符为prop
export const getParentNodeList = (treeData: TreeDataType[], target: any, prop: string): TreeDataType[] => {
	const result: TreeDataType[] = [];

	const findNode = (nodes: TreeDataType[], path: TreeDataType[]): boolean => {
		for (const node of nodes) {
			// Push the current node to the path
			path.push(node);

			// Check if the current node is the target node
			if (node[prop] === target[prop]) {
				// If it is, copy the current path to the result and return true
				result.push(...path);
				return true;
			}

			// If the current node has children, recursively search in the children
			if (node.children) {
				if (findNode(node.children, path)) {
					return true;
				}
			}

			// If we haven't found the target node, pop the current node from the path
			path.pop();
		}
		// If we have traversed all nodes and haven't found the target node, return false
		return false;
	};

	findNode(treeData, []);

	return result;
};
